﻿#include "game.h"

//初始化棋盘
void Bord_init(char arr[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	int j = 0;

	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			arr[i][j] = set;
		}
	}
}

//打印棋盘
void Display_pr(char arr[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);
		for (j = 1; j <= col; j++)
		{
			printf("%c ", arr[i][j]);
		}
		printf("\n");
	}
}

//设置地雷
void Set_mine(char arr[ROWS][COLS], int row, int col)

{

	int count = BOW;
	while (count)
	{
		int i = rand() % row + 1;
		int j = rand() % col + 1;
		if (arr[i][j] == '0')
		{
			arr[i][j] = '1';
			count--;
		}
	}
}

//计算周围地雷
int Count_mine(char mine[ROWS][COLS], int x, int y)
{

	int i = 0;
	int j = 0;
	int c = 0;
	for (i = 1; i >= -1; i--)
	{
		for (j = 1; j >= -1; j--)
		{

			if (mine[x - i][y - j] == '2') 
			{
				continue;
			}
			c += mine[x - i][y - j] - '0';
		}
	}
	return c;
}



//用户显示界面
int Diguizhankai(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
{
	int i = 0;
	int j = 0;
	int c = 0;
	if (x < 1 || y > COL || x > ROW || y < 1)
		return 0;
	c = Count_mine(mine, x, y);
	if (c == 0)
	{
		mine[x][y] = '2';
		for (i = 1; i >= -1; i--)
		{
			for (j = 1; j >= -1; j--)
			{
				show[x - i][y - j] = ' ';
			}
		}
		for (i = 1; i >= -1; i--)
		{
			for (j = 1; j >= -1; j--)
			{
				if (mine[x - i][y - j] == '2')
				{
					continue;
				}
				Diguizhankai(mine, show, ROW, COL, x - i, y - j);
			}
		}
	}
	else 
	{
		show[x - i][y - j] = '0' + c;
	}
}

//计算'*'
int Count_tiaojian(char show[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	int c = 0;

	for (i = 1; i <= row; i++)
	{
		for (j = 1; j <= col; j++)
		{
			if (show[i][j] == '*'|| show[i][j] == 'X')
			{
				c++;
			}
		}
	}
	return c;
}


//排查地雷
void Find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	char mos = 'i';
	int count = Count_tiaojian(show, row, col);
	while (count>BOW)
	{
		system("cls");
		Display_pr(show, row, col);
		//Display_pr(mine, row, col);
		printf("模式x标记，模式i排雷\n");
		printf("坐标 坐标 模式\n");
		printf("请输入坐标:");
		scanf("%d %d %c", &x, &y, &mos);
		
		if (mos == 'x')
		{
			if (show[x][y] == '*')
			{
				show[x][y] = 'X';
			}
			continue;
		}
		if (x<1 || x>row || y<1 || y>col)
		{
			printf("你输入的坐标有误！\n");
			Sleep(1000);
			continue;
		}
		else
		{
			if (mine[x][y] == '1')
			{
				printf("boow！地雷炸了!\n");
				Display_pr(mine, row, col);
				Sleep(3000);
				break;
			}
			else
			{
				Diguizhankai(mine, show, ROW, COL, x, y);
			}
		}
		count = Count_tiaojian(show, row, col);
		if (count == BOW) {
			printf("恭喜你通关了!\n");
			Display_pr(mine, row, col);
			Sleep(5000);
		}
	}
}

